package com.hongawen.thread;

import cn.hutool.core.io.FileUtil;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年08月13日 18:54
 */
public class ThreadDownNetPics extends Thread{

    private String url;

    private String name;

    public ThreadDownNetPics(String url,String name){
        this.url=url;
        this.name=name;
    }

    /**
     * 下载网络图片
     */
    @SneakyThrows
    @Override
    public void run() {
        this.webDownPic(url,name);
        System.out.println("下载了图片，图片名为："+name);
    }

    public static void main(String[] args) {
        ThreadDownNetPics t1 = new ThreadDownNetPics("https://tse2-mm.cn.bing.net/th/id/OIP-C.xsA-3qUw6cqmd8nRfxk6TQHaEK?w=308&h=180&c=7&o=5&pid=1.7","1.png");
        ThreadDownNetPics t2 = new ThreadDownNetPics("https://tse1-mm.cn.bing.net/th/id/OIP-C.FaG6dzohGs3q45-DwsEyQQHaEK?w=308&h=180&c=7&o=5&pid=1.7","2.jpg");
        ThreadDownNetPics t3 = new ThreadDownNetPics("https://tse3-mm.cn.bing.net/th/id/OIP-C.5OQ1UC4qRDudD0FFKPcB9QHaEB?w=325&h=180&c=7&o=5&pid=1.7","3.png");
        t1.setPriority(1);
        t1.start();
        t2.start();
        t3.start();
    }

    public void webDownPic(String url,String name) throws IOException {
        FileUtils.copyURLToFile(new URL(url),new File(name));
    }

}
